<?php

function	get_directories($dir)
{
	if (is_dir($dir))
	{
		if ($dh = opendir($dir))
		{
			$directories = array();

			while (($file = readdir($dh)) !== false)
			{
				if (filetype($dir.$file) == "dir" && $file !== "." && $file !== "..")
					$directories[filetime($file)] = $file;
			}
			krsort($directories);
			closedir($dh);
			return ($directries);
		}
	}
	return (0);
}

function	is_successful($dir)
{
	$ret = 0;
	$end_status = fopen($dir."/end_status.txt", "r");
	if ($end_status)
	{
		if (fgets($end_status) && ($str = fgets($end_status)) && $str == "0\n")
			$ret = 1;
		fclose($end_status);
	}
	return ($ret);
}

if ($_GET["show"])
{
	$dir = $_GET["show"];
	if (is_dir($dir))
	{
		echo "<textarea cols=\"40\" rows=\"25\">";
		readfile($dir."/stdout.txt");
		echo "</textarea>
<textarea cols=\"40\" rows=\"25\">";
		readfile($dir."/stderr.txt");
		echo "</textarea>";
	}
}
else
{
	echo "<html>
	<head>
	</head>
	<body>
		<table border=\"1\">
			<tr>
				<th>Name</th>
				<th>Success</th>
			</tr>
";

	$rootdir = "./";
	$directories = get_directories($rootdir);
	foreach ($dir in $directories)
	{
		if (is_successful($rootdir.$file))
			echo "			<tr bgcolor=\"#00FF00\"><td><a href=\"index.php?show=".$rootdir.$file."\">".$dir."</a></td><td>YES</td></tr>\n";
		else
			echo "			<tr bgcolor=\"#FF0000\"><td><a href=\"index.php?show=".$rootdir.$file."\">".$dir."</a></td><td>NO</td></tr>\n";
	}
	echo "		</table>
	</body>
</html>
";
}

?>
